﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Globalization;
using Recon.Portal.Business;
using Recon.Portal.DataObjects;

namespace Recon.Portal.WebUI
{
    public partial class RoleMaster : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            User currentUser = ((User)Session["CurrentUserVO"]);
            if (currentUser == null)
            {
                Response.Redirect("ReconSignIn.aspx");
            }

            if (!IsPostBack)
            {
                if (currentUser != null)
                {
                    if (currentUser.UserType != "S")
                    {
                        ddlRoleType.Items.RemoveAt(1);
                    }
                }
                //bntbrowse.Enabled = false;
                //bntgetdetails.Enabled = false;
                //rbOption.Focus();
                BindGrid();
            }
        }
        public void BindGrid()
        {
            RoleEntity roleEntity = new RoleEntity();
            List<UserRole> roleLst = null;

            try
            {
                roleLst = roleEntity.GetAllRoles(((User)Session["CurrentUserVO"]).UserType == "S", ((User)Session["CurrentUserVO"]).CustomerID);
                   
                   // role = roleEntity.GetRoleDetailsByRoleId(txtRoleID.Text.Trim(), 2);

                if (roleLst == null)
                {
                    gvCustomer.DataSource = null;
                    gvCustomer.DataBind();
                }
                else
                {
                    gvCustomer.DataSource = roleLst;
                    gvCustomer.DataBind();
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        //protected void bntbrowse_Click(object sender, EventArgs e)
        //{
        //   // ClientScript.RegisterClientScriptBlock(this.GetType(), "", "<SCRIPT>window.open('SearchRemitter.aspx?textbox=" + txtRemitterId.Text + "&PageRequest= RoleMaster.aspx','cal','height=400, width=650, left=300, top=150, location=no, menubar=no, resizable=no,scrollbars=no, titlebar=no, toolbar=no',false);</SCRIPT>");
        //    ClientScript.RegisterStartupScript(this.GetType(), "", "<SCRIPT>window.open('SearchRoleCode.aspx?textbox=" + txtRoleID.Value + 
        //        "&PageRequest= RoleMaster.aspx','cal','height=400, width=650, left=300, top=150, location=no, menubar=no, resizable=no,scrollbars=no, titlebar=no, toolbar=no',false);</SCRIPT>");
        //       // "','_Parent','height=400, width=650, left=300, top=150, location=no, menubar=no, resizable=no,scrollbars=no, titlebar=no, toolbar=no',false);</SCRIPT>");
        //    txtRoleID.Disabled = false;
        //}

        //protected void bntgetdetails_Click(object sender, EventArgs e)
        //{
        //    RoleEntity roleEntity = new RoleEntity();
        //    UserRole role;

        //    try
        //    {
        //        txtRoleID.Disabled = false;
        //        if (txtRoleID.Value.Trim() != "")
        //        {
        //            role = roleEntity.GetRoleDetailsByRoleId(Convert.ToString(txtRoleID.Value.Trim()), 1);

        //            if (role == null)
        //            {
        //                btnClickPopup.Show();
        //                lblMessage.Text = "No record found for this role!";
        //            }
        //            else
        //            {
        //                ResetFields();
        //                txtRoleID.Value = role.RoleID;
        //                txtRoleDesc.Text = role.RoleDescription;
        //                ddlRoleType.ClearSelection();
        //                ddlRoleType.Items.FindByValue(role.RoleType).Selected = true;
        //                    chkEnable.Checked = role.IsEnabled;
        //                rbOption.SelectedValue = "2";
        //                txtRoleID.Disabled = true;
        //            }
        //        }
        //        else
        //        {
        //            btnClickPopup.Show();
        //            lblMessage.Text = "Enter Role ID!";
        //        }
        //    }
        //    catch (Exception ex)
        //    {
        //        if (ex != null)
        //        {
        //            throw;
        //        }
        //    }
        //}

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            string resp = string.Empty;
            RoleEntity roleEntity = new RoleEntity();
            UserRole role;
            //if (rbOption.SelectedValue == "1")
            //{
                role = roleEntity.GetRoleDetailsByRoleId(txtRoleID.Value.Trim(), 1);

                if (role != null)
                {
                    role = new UserRole
                    {
                        RoleID = txtRoleID.Value.Trim(),
                        RoleDescription = txtRoleDesc.Text.Trim(),
                        RoleType = ddlRoleType.SelectedValue,
                        IsEnabled = chkEnable.Checked,
                        CustomerID = ((User)Session["CurrentUserVO"]).CustomerID
                    };
                    int retval = roleEntity.ModifyRole(role, ((User)Session["CurrentUserVO"]).UserID);
                    if (retval != 0)
                    {
                        lblMessage.Text = "Role modified successfully!";
                        btnClickPopup.Show();
                        ResetFields();
                    }
                    else
                    {
                        lblMessage.Text = "Error Modifying role!";
                        btnClickPopup.Show();
                    }
                }
                else
                {
                    role = new UserRole
                    {
                        RoleID = txtRoleID.Value.Trim(),
                        RoleDescription =  txtRoleDesc.Text.Trim(),
                        RoleType = ddlRoleType.SelectedValue,
                        IsEnabled = chkEnable.Checked,
                        CustomerID = ((User)Session["CurrentUserVO"]).CustomerID
                    };
                    int retval = roleEntity.RegisterRole(role, ((User)Session["CurrentUserVO"]).UserID);
                    if (retval != 0)
                    {
                        btnClickPopup.Show();
                        lblMessage.Text = "Role added successfully!";
                        ResetFields();
                    }
                    else
                    {
                        btnClickPopup.Show();
                        lblMessage.Text = "Error adding role!";
                    }
                }
                BindGrid();
            /*}
            else
            {
                txtRoleID.Disabled = false;
				role = roleEntity.GetRoleDetailsByRoleId(Convert.ToString(txtRoleID.Value.Trim()), 1);
                if (role == null)
                {
                    btnClickPopup.Show();
                    lblMessage.Text = "Role Code does not exist!";
                }
                else
                {
                    role = new UserRole
                    {
                        RoleID = txtRoleID.Value.Trim(),
                        RoleDescription = txtRoleDesc.Text.Trim(),
                        RoleType = ddlRoleType.SelectedValue,
                        IsEnabled = chkEnable.Checked
                    };
                    int retval = roleEntity.ModifyRole(role, ((User)Session["CurrentUserVO"]).UserID);
                    if (retval != 0)
                    {
                        lblMessage.Text = "Role modified successfully!";
                        btnClickPopup.Show();
                        ResetFields();
                    }
                    else
                    {
                        lblMessage.Text = "Error Modifying role!";
                        btnClickPopup.Show();
                    }
                }
            }*/
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            ResetFields();
        }

        private void ResetFields()
        {
            txtRoleID.Value = "";
            ddlRoleType.SelectedValue = "0";
            txtRoleDesc.Text = "";            
            chkEnable.Checked = true;
            txtRoleID.Disabled = false;
            btnSubmit.Text = "Add";
        }

        protected void btnok_Click(object sender, EventArgs e)
        {
            btnClickPopup.Hide();
        }

        protected void gvCustAccount_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            int retval = 0;
            try
            {
                Label RoleID = ((Label)gvCustomer.Rows[e.RowIndex].Cells[1].FindControl("lblRoleID"));
                RoleEntity roleEntity = new RoleEntity();
                retval = roleEntity.DeleteRole(Convert.ToString(RoleID.Text));
                if (retval != 0)
                {
                    lblMessage.Text = "Role detail deleted successfully!";
                    btnClickPopup.Show();
                }
                else
                {
                    lblMessage.Text = "Error in deleting role detail!";
                    btnClickPopup.Show();
                }
            }
            catch
            {

            }
            BindGrid();
        }

        //protected void rbOption_SelectedIndexChanged(object sender, EventArgs e)
        //{
        //    if (rbOption.SelectedValue.Trim() == "1")
        //    {
        //        bntbrowse.Enabled = false;
        //        bntgetdetails.Enabled = false;
        //        txtRoleID.Disabled =  false;
        //    }
        //    else
        //    {
        //        bntbrowse.Enabled = true;
        //        bntgetdetails.Enabled = true;
        //        txtRoleID.Disabled = true;
        //    }
        //}
    }
}